package com.dyzx.hbb.modules.wms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.modules.sys.entity.SysMenu;
import com.dyzx.hbb.modules.wms.entity.WmsProductCategory;
import com.dyzx.hbb.modules.wms.service.WmsProductCategoryService;
import com.dyzx.hbb.modules.wms.mapper.WmsProductCategoryMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【wms_product_category】的数据库操作Service实现
* @createDate 2025-06-01 17:40:28
*/
@Service
public class WmsProductCategoryServiceImpl extends ServiceImpl<WmsProductCategoryMapper, WmsProductCategory>
    implements WmsProductCategoryService{
    @Override
    public List<Map<String, Object>> findByTreeList() {
        // 1. 查询所有菜单列表
        List<WmsProductCategory> menuList = this.list();

        // 2. 构建树形结构
        // 获取顶级菜单

        return menuList.stream()
                .filter(menu -> menu.getParentId() == 0)  // 获取顶级菜单
                .map(menu -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", menu.getId());
                    map.put("name", menu.getName());
                    map.put("create_date", menu.getCreateDate());
                    map.put("update_date", menu.getUpdateDate());
                    map.put("parent_id", menu.getParentId());
                    map.put("status", menu.getStatus());

                    // 递归获取子菜单
                    List<Map<String, Object>> children = getChildren(menu, menuList);
                    if (!children.isEmpty()) {
                        map.put("children", children);
                    }
                    return map;
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<Map<String, Object>> findByMiniList() {
        // 1. 查询所有菜单列表
        List<WmsProductCategory> menuList = this.list();

        // 2. 构建树形结构
        // 获取顶级菜单

        return menuList.stream()
                .filter(menu -> menu.getParentId() == 0)  // 获取顶级菜单
                .map(menu -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("value", menu.getId());
                    map.put("label", menu.getName());

                    // 递归获取子菜单
                    List<Map<String, Object>> children = getChildrenmMini(menu, menuList);
                    if (!children.isEmpty()) {
                        map.put("children", children);
                    }
                    return map;
                })
                .collect(Collectors.toList());
    }
    private  List<Map<String, Object>> getChildrenmMini(WmsProductCategory menu, List<WmsProductCategory> menuList) {
        // 递归查找子菜单
        return menuList.stream()
                .filter(m -> m.getParentId().equals(menu.getId()))  // 过滤出当前菜单的子菜单
                .map(m -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("value", m.getId());
                    map.put("label", m.getName());

                    // 递归获取子菜单
                    List<Map<String, Object>> children = getChildren(m, menuList);
                    if (!children.isEmpty()) {
                        map.put("children", children);
                    }
                    return map;
                })
                .collect(Collectors.toList());
    }

    private List<Map<String, Object>> getChildren(WmsProductCategory menu, List<WmsProductCategory> menuList) {
        // 递归查找子菜单
        return menuList.stream()
                .filter(m -> m.getParentId().equals(menu.getId()))  // 过滤出当前菜单的子菜单
                .map(m -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", menu.getId());
                    map.put("name", menu.getName());
                    map.put("create_date", menu.getCreateDate());
                    map.put("update_date", menu.getUpdateDate());
                    map.put("parent_id", menu.getParentId());
                    map.put("status", menu.getStatus());
                    // 递归获取子菜单
                    List<Map<String, Object>> children = getChildren(m, menuList);
                    if (!children.isEmpty()) {
                        map.put("children", children);
                    }
                    return map;
                })
                .collect(Collectors.toList());
    }
}




